﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;
using System.Text.RegularExpressions;
using System.Data.OleDb;

namespace Serverslist
{
    public partial class Download_Server : MetroForm
    {
        
        public Download_Server()
        {
            InitializeComponent();
        }

        private void Download_Server_Load(object sender, EventArgs e)
        {

        }

        private void check_Click(object sender, EventArgs e)
        {
            if (servercheck())
            {

                MessageBox.Show("Done !");



            }
            else 
            {

                MessageBox.Show("SomeThing Wrong !");
            
            
            
            }
        }
        private Boolean servercheck() 
        {
            string link = "http://servers.travibot.com/?domain=" + domaintext.Text + "&l=en";
            Web page = new Web();
            string serverpages = page.getserverpage(link);

            if(serverpages != null && serverpages != "")
            {


                    string pattern = "<a href=\"Http://\\w+\\d+." + domaintext.Text + "/\"";
                    MatchCollection servers = Regex.Matches(serverpages, pattern, RegexOptions.IgnoreCase); 

                    foreach (Match serverss in servers)
                    {
                        string serverlink = serverss.Value;
                        serverlink = serverlink.Replace("<a href=\"", "");
                        serverlink = serverlink.Replace("/\"", "");
                        listView1.Items.Add(new ListViewItem(new string[] {serverlink}));    
                    }
                    return true;
            }
            else
            {
                return false;
            }
        }

        private void savetodb_Click(object sender, EventArgs e)
        {
            string msg = "Server Add Succefully !";
           // string procces = string.Format("Insert into ServerList values ('{0}')";
            listviewadd(msg);
          
        }
        
        private void Dataproccess(string procces,string msg)
        {

            // 0. DataBase Variables .
            OleDbConnection conn = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();

            //1. requirements .
            conn.ConnectionString = "Provider=SQLNCLI11;Data Source=DODY\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=TravianServers1";
            cmd.Connection = conn;
            cmd.CommandText = string.Format(procces);


            // 2. DbWork.
            try
            {
                conn.Open();
                var x = cmd.ExecuteNonQuery();
                MessageBox.Show(msg);
                conn.Close();
            }
            catch (OleDbException ex)
            {

                string exx = ex.ToString();
                MessageBox.Show("Something Wrong .");


            }


        }
        private void listviewadd(string msg)
        {
            // 0. DataBase Variables .
            OleDbConnection conn = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();

            //1. requirements .
            conn.ConnectionString = "Provider=SQLNCLI11;Data Source=DODY\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=TravianServers1";
            cmd.Connection = conn;
            


            // 2. DbWork.
            try
            {

                conn.Open();
                foreach (ListViewItem li in listView1.Items)
                {
                    cmd.CommandText = "insert into ServerList (ServerUrl)values('" + li.SubItems[0].Text + "')";
                    cmd.ExecuteNonQuery();

                }
                
                MessageBox.Show(msg);
                conn.Close();
            }
            catch (OleDbException ex)
            {

                string exx = ex.ToString();
                MessageBox.Show("Something Wrong .");


            }

         
        
        
        }
    }

}
